﻿using System;
using System.Data;
using NUnit.Framework;

namespace Azalea.Test.ORMap
{
    [TestFixture]
    public class CommandExecutorTester : Config
    {
        [Test]
        public void TestDataRowExecute()
        {
            IDbCommand cmd =
                _cmdBuilder.Select().Table("ANONYMIZATION_LOG").AllFields().Where().Equals("ID", 46).Done().Done(true);
            Console.WriteLine(cmd.CommandText);
            DataRow dr = _executor.DataRowExecute(cmd);
            Assert.IsNotNull(dr);
        }

        [Test]
        public void TestDataSetExecute()
        {
            IDbCommand cmd = _cmdBuilder.Select().Table("ANONYMIZATION_LOG").AllFields().Done().Done(true);
            DataSet ds = _executor.DataSetExecute(cmd);
            Assert.AreEqual(1, ds.Tables.Count);
        }

        [Test]
        public void TestDataTableExecute()
        {
            IDbCommand cmd = _cmdBuilder.Select().Table("ANONYMIZATION_LOG").AllFields().Done().Done(true);
            DataTable dt = _executor.DataTableExecute(cmd);
            Assert.IsNotNull(dt);
        }


        [Test]
        [Ignore]
        public void TestNonQueryExecute()
        {
            const int id = 46;
            DateTime time = DateTime.Now;
            IDbCommand cmd = _cmdBuilder.Update().Table("ANONYMIZATION_LOG").Set("CREATE_DATETIME", time)
                .Where().Equals("ID", id).Done().Done(true);
            _executor.NonQueryExecute(cmd);

            IDbCommand cmd2 =
                _cmdBuilder.Select().Table("ANONYMIZATION_LOG").Field("CREATE_DATETIME").Where().Equals("ID", id).Done()
                    .Done(true);
            DataRow dr = _executor.DataRowExecute(cmd2);
            Assert.AreEqual(time, dr[0]);
        }

        [Test]
        public void TestScalarExecute()
        {
            IDbCommand cmd = _cmdBuilder.Select().Table("ANONYMIZATION_LOG").Count().Done().Done(true);
            var cnt = (decimal) _executor.ScalarExecute(cmd);
            Assert.Less(0, cnt);
        }
    }
}